package com.sto.stoapp.broadcast;

import com.sto.stoapp.db.StoFilePreference;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;

public class BatteryInfoReceive extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
			// 是否在充电
			int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
			boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
					|| status == BatteryManager.BATTERY_STATUS_FULL;
			// 获取电池剩余电量
			int level1 = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

			StoFilePreference.getInstance(context).setBatteryLevel(level1 + "");
			if (isCharging) {
				StoFilePreference.getInstance(context).setBatteryState("1");
			} else {
				StoFilePreference.getInstance(context).setBatteryState("0");
			}


			Log.i("BatteryInfoReceive", "是否在充电:" + isCharging);

			Log.i("BatteryInfoReceive", "获取电池剩余电量:" + level1);
		}
	}
}
